package cn.elwy.kit.activity.dialog;

import java.io.File;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.elwy.kit.activity.service.RandomGrouping;

public class AppMain {

    protected Shell shell;
    private Text srcText;
    private Text destText;
    private Button srcButton;
    private Button destButton;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            AppMain window = new AppMain();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        shell.setLayout(new GridLayout(3, false));

        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        lblNewLabel.setText("源文件：");

        srcText = new Text(shell, SWT.BORDER);
        srcText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        srcButton = new Button(shell, SWT.NONE);
        srcButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String fileDig = fileDig(shell);
                srcText.setText(fileDig);
            }
        });
        srcButton.setText("选择");

        Label lblNewLabel_1 = new Label(shell, SWT.NONE);
        lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        lblNewLabel_1.setText("输出文件：");

        destText = new Text(shell, SWT.BORDER);
        destText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        destButton = new Button(shell, SWT.NONE);
        destButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String fileDig = fileDig(shell);
                destText.setText(fileDig);
            }
        });
        destButton.setText("选择");
        new Label(shell, SWT.NONE);

        Button okButton = new Button(shell, SWT.NONE);
        GridData gd_okButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_okButton.widthHint = 60;
        okButton.setLayoutData(gd_okButton);
        okButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent event) {
                String srcPath = srcText.getText();
                String destPath = destText.getText();
                try {
                    if (!srcPath.toLowerCase().endsWith("xls") && !srcPath.toLowerCase().endsWith("xlsx")) {
                        throw new RuntimeException("请选择EXCEL文件！");
                    }
                    if (!destPath.toLowerCase().endsWith("xls") && !destPath.toLowerCase().endsWith("xlsx")) {
                        destPath = destPath + "." + getExtName(srcPath);
                    }
                    File srcFile = new File(srcPath);
                    if (!srcFile.exists()) {
                        throw new RuntimeException(srcPath + "不存在！");
                    }
                    File destFile = new File(destPath);
                    if (destFile.isDirectory()) {
                        throw new RuntimeException(destPath + "不可以是目录！");
                    }

                    RandomGrouping f = new RandomGrouping(srcFile, destFile, 10);
                    f.loadData();
                    f.saveData();
                    MessageDialog.openInformation(shell, "错误", "文件输出成功！");
                    return;
                } catch (Exception e) {
                    MessageDialog.openError(shell, "错误", e.getMessage());
                    return;
                }
            }
        });
        okButton.setText("确认");
        new Label(shell, SWT.NONE);

    }

    protected String fileDig(Shell parent) {
        // 新建文件对话框，并设置为打开的方式
        FileDialog filedlg = new FileDialog(parent, SWT.OPEN);
        // 设置文件对话框的标题
        filedlg.setText("文件选择");
        // 设置初始路径
        filedlg.setFilterPath("SystemRoot");
        // 打开文件对话框，返回选中文件的绝对路径
        String selected = filedlg.open();

        return selected;
    }

    public static String getExtName(String fileName) {
        int index = fileName.lastIndexOf(".");
        return index != -1 ? fileName.substring(index + 1) : "";
    }
}
